import { EnergyMgr } from "./EnergyMgr";
import { EventKey } from "../common/Key";

cc.Class({
    extends: cc.Component,

    properties: {
        labEnergy:cc.Label,
        labTime:cc.Label,
    },

    // onLoad () {},

    start () {
        this.node.on('click',()=>{
            G.partner.sendEvent('打开弹窗', { '类型': '增加能量' });
            G.ui.openUI('PanelAddEnergy');
        })

        this._updateEnergyUI();
        this._registEvent();
    },

    _updateEnergyUI() {
        this.labEnergy.string = EnergyMgr.getFormatEnergy();
        this.labTime.string = EnergyMgr.getFormatCountDown();
    },

    onDestroy() {
        this._unRegistEvent();
    },

    _registEvent() {
        G.onFire.on(EventKey.EVENT_TIME_CHANGE, this._onFire, this);
        G.onFire.on(EventKey.EVENT_ENERGY_CHANGE, this._onFire, this);

    },
    _unRegistEvent() {
        G.onFire.off(EventKey.EVENT_TIME_CHANGE, this._onFire, this);
        G.onFire.off(EventKey.EVENT_ENERGY_CHANGE, this._onFire, this);
    },

    _onFire(eventName, param) {
        if (eventName == EventKey.EVENT_TIME_CHANGE) {
            this._updateEnergyUI();
        } else if (eventName == EventKey.EVENT_ENERGY_CHANGE) {
            this._updateEnergyUI();
        }
    },
    // update (dt) {},
});
